package cn.hhchat.kit.http;

import com.alibaba.fastjson.JSON;
import com.sun.net.httpserver.BasicAuthenticator;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created this one by HMH on 2017/3/23.
 */
public class httpUtil {

    private final static Logger logger = LoggerFactory.getLogger(httpUtil.class);

    private static OkHttpClient client;

    public static OkHttpClient getClient() {
        if (client != null) {
            return client;
        }
        client = new OkHttpClient();
        return client;
    }

    public static Response post(String url, Object... params) throws IOException {
        Response response = null;
        int length = params.length/2*2;
        FormBody.Builder paramBuilder = new FormBody.Builder();
        for(int i=0;i<length;i+=2) {
            paramBuilder.add(params[i].toString(), params[i + 1].toString());
        }
        Request request = new Request.Builder()
                .url(url)
                .post(paramBuilder.build())
                .build();
        response = getClient().newCall(request).execute();
        return response;
    }

    /**
     * 添加头的postJson方法
     */
    public static Response postJson(String url, Map<String,String> headers, String jsonString) throws IOException {
        Request.Builder builder = new Request.Builder();
        for (Map.Entry<String, String> en : headers.entrySet()) {
            builder.addHeader(en.getKey(), en.getValue());
        }
        RequestBody requestBody = RequestBody.create(JSONType, jsonString);
        Request request = builder.url(url).post(requestBody).build();

        Response response;

        response = getClient().newCall(request).execute();
        if (response.isSuccessful()) {
            return response;
        }
        return null;
    }

    public static Response postJson(String url, Map<String,String> headers, Object object) throws IOException {
        return postJson(url,headers,JSON.toJSONString(object));
    }

    private static MediaType JSONType = MediaType.parse("application/json; charset=utf-8");

    public static Response postJson(String url, Object object) throws IOException {
        return postJson(url, JSON.toJSONString(object));
    }

    public static Response postJson(String url, String jsonString) throws IOException {

        RequestBody requestBody = RequestBody.create(JSONType, jsonString);
        Request request = new Request.Builder().url(url).post(requestBody).build();

        Response response = null;

        response = getClient().newCall(request).execute();
        if (response.isSuccessful()) {
            return response;
        }
        return null;
    }

    public static Response get(String url) throws IOException {
        Request request = new Request.Builder().url(url).build();
        Response response = null;

        response = getClient().newCall(request).execute();
        if (response.isSuccessful()) {
            return response;
        }
        return null;
    }

    public static Response postJsonWithBasicAutho(String url, String name,String pwd , Object object) throws IOException {
        String credential = Credentials.basic(name, pwd);
        Map<String, String> map = new HashMap<String, String>();
        map.put("Authorization", credential);
        return postJson(url, map, object);

    }

    public static Response postWithBasicAutho(String url, String name,String pwd , Object... params) throws IOException {
        String credential = Credentials.basic(name, pwd);

        Response response = null;
        int length = params.length/2*2;
        FormBody.Builder paramBuilder = new FormBody.Builder();
        for(int i=0;i<length;i+=2) {
            paramBuilder.add(params[i].toString(), params[i + 1].toString());
        }
        Request request = new Request.Builder()
                .url(url).addHeader("Authorization",credential)
                .post(paramBuilder.build())
                .build();
        response = getClient().newCall(request).execute();
        return response;

    }


}
